2023-10-17


Typescript type casting
Typescript has something called type casting which can be used if you want a more specific type.
For example
const myForm = document.querySelector("#my-form");
would give an Element type but maybe we want a more specific type. We could do type casting:
const myForm = document.querySelector("#my-form") as HTMLFormElement;
If we do this it’s more apparent that myForm really is a HTML form element.
We can do the same thing with the e in an event callback function which by default has an any type. By doing this instead e: UIEvent we make it obvious that we’re working with some kind of UI event.
Go back to all posts